# coding: utf-8
import datetime

menu = [
    [1, '家电', [[1, '海尔洗衣机', 123], [2, '三星冰箱', 234], [3, '索尼电视', 345]]],
    [2, '衣服', [[1, '优衣库衬衫', 99], [2, 'H&M卫衣', 129], [3, '李维斯牛仔裤', 899]]],
    [3, '手机', [[1, '苹果 iPhone', 6088], [2, '三星 Galaxy', 4988], [3, '魅族 Pro6', 2488]]],
    [4, '汽车', [[1, '宝马 M3', 899000], [2, '奥迪 S7', 1010000], [3, '大众 CC', 310000]]],
]


def list_first_menu():
    """

    :return: 编号 or 操作符
    """
    for i in menu:
        print(i[0], '\t', i[1])

    while True:
        print("全局操作符:")
        print("[Q]退出 [C]查看历史及本次购物清单")
        print("[T]充值 [L]查看本次购物清单")
        print("输入序号进入二级菜单")
        oper = input("> ").strip()
        if oper.isdigit() and check_first_index(oper) == True:
            return oper
        elif oper.upper() == "Q" or oper.upper() == "C" or oper.upper() == "L" or \
                oper.upper() == "T":
            return oper.upper()
        else:
            print("输入异常!重新输入!")


def list_second_menu(int_index):
    """

    :param int_index: 一级菜单编号
    :return: 二级菜单编号
    """
    index = int(int_index) - 1
    goods = []
    for i in range(2, len(menu[index][2])):
        goods.append(menu[index][i])

    for i in goods:
        for j in i:
            print(j[0], '\t', j[1], '\t', j[2])

    while True:
        print("全局操作符:")
        print("[Q]退出 [C]查看历史及本次购物清单 [L]查看本次购物清单")
        print("[T]充值 [B]返回上级菜单")
        print("输入序号进行购买")
        oper = input("> ").strip()
        num = '1'
        if oper.isdigit():
            num = input("输入购买数量: ").strip()
        if num.isdigit() and oper.isdigit() and check_second_index(oper, int_index) == True:
            return [str(datetime.datetime.now()), get_goods_name(int_index, oper), get_price(int_index, oper), num]

        elif oper.upper() == "Q" or oper.upper() == "C" or oper.upper() == "L" or \
                oper.upper() == "T" or oper.upper() == "B":
            return oper.upper()
        else:
            print("输入异常!重新输入!")


def check_first_index(num):
    """
    检查参数中的数字是否超出了第一级菜单的索引
    :param num:
    :return:
    """
    len_menu = len(menu)
    if 0 < int(num) <= len_menu:
        return True
    else:
        return False


def check_second_index(num, menu_index):
    """
    检查参数中的数字是否超出了指定第二级菜单的索引
    :param num:
    :param menu_index: 第二级菜单的编号
    :return:
    """
    len_menu = len(menu[int(menu_index)-1])
    if 0 < int(num) <= len_menu:
        return True
    else:
        return False


def get_price(first_index, second_index):
    """

    :param first_index:
    :param second_index:
    :return: price
    """
    return menu[int(first_index) - 1][2][int(second_index) - 1][2]


def get_goods_name(first_index, second_index):
    """

    :param first_index:
    :param second_index:
    :return: price
    """
    return menu[int(first_index) - 1][2][int(second_index) - 1][1]


if __name__ == "__main__":
    # list_first_menu()
    # print list_second_menu(1)
    # print check_first_index(0)
    # print check_second_index(4, 2)
    # print list_first_menu()
    # print list_second_menu(1)
    # print get_price(1,2)
    pass